package com.sas.repository;

import com.sas.entity.Schedule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * 排班Repository接口
 */
@Repository
public interface ScheduleRepository extends JpaRepository<Schedule, Long> {

    /**
     * 根据员工ID查询排班
     */
    List<Schedule> findByEmployeeId(Long employeeId);

    /**
     * 根据房间ID查询排班
     */
    List<Schedule> findByRoomId(Long roomId);

    /**
     * 根据客户ID查询排班
     */
    List<Schedule> findByCustomerId(Long customerId);

    /**
     * 根据排班日期查询排班
     */
    List<Schedule> findByScheduleDate(LocalDate scheduleDate);

    /**
     * 根据员工ID和日期查询排班
     */
    List<Schedule> findByEmployeeIdAndScheduleDate(Long employeeId, LocalDate scheduleDate);

    /**
     * 根据房间ID和日期查询排班
     */
    List<Schedule> findByRoomIdAndScheduleDate(Long roomId, LocalDate scheduleDate);

    /**
     * 根据日期范围查询排班
     */
    @Query("SELECT s FROM Schedule s WHERE s.scheduleDate >= :startDate AND s.scheduleDate <= :endDate")
    List<Schedule> findByDateRange(@Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    /**
     * 根据员工ID和日期范围查询排班
     */
    @Query("SELECT s FROM Schedule s WHERE s.employeeId = :employeeId AND s.scheduleDate >= :startDate AND s.scheduleDate <= :endDate")
    List<Schedule> findByEmployeeIdAndDateRange(@Param("employeeId") Long employeeId, 
                                               @Param("startDate") LocalDate startDate, 
                                               @Param("endDate") LocalDate endDate);

    /**
     * 根据房间ID和日期范围查询排班
     */
    @Query("SELECT s FROM Schedule s WHERE s.roomId = :roomId AND s.scheduleDate >= :startDate AND s.scheduleDate <= :endDate")
    List<Schedule> findByRoomIdAndDateRange(@Param("roomId") Long roomId, 
                                           @Param("startDate") LocalDate startDate, 
                                           @Param("endDate") LocalDate endDate);

    /**
     * 根据状态查询排班
     */
    List<Schedule> findByStatus(Integer status);

    /**
     * 根据班次类型查询排班
     */
    List<Schedule> findByShiftType(Integer shiftType);

    /**
     * 计算员工在指定日期的总工作小时数
     */
    @Query("SELECT COALESCE(SUM(s.workHours), 0) FROM Schedule s WHERE s.employeeId = :employeeId AND s.scheduleDate = :scheduleDate AND s.status != 0")
    Double getTotalWorkHoursByEmployeeAndDate(@Param("employeeId") Long employeeId, @Param("scheduleDate") LocalDate scheduleDate);

    /**
     * 注意：算法相关接口统一改为使用实体中的 scheduleDate 字段
     * 可使用 findByScheduleDate 和 findByDateRange
     */

    /**
     * 查询员工在指定日期和时间段的冲突排班（基于 employeeId 与 scheduleDate）
     */
    @Query("SELECT s FROM Schedule s WHERE s.employeeId = :employeeId AND s.scheduleDate = :date " +
           "AND ((s.startTime <= :startTime AND s.endTime > :startTime) " +
           "OR (s.startTime < :endTime AND s.endTime >= :endTime) " +
           "OR (s.startTime >= :startTime AND s.endTime <= :endTime))")
    List<Schedule> findByEmployeeIdAndDateAndTimeOverlap(@Param("employeeId") Long employeeId,
                                                         @Param("date") LocalDate date,
                                                         @Param("startTime") java.time.LocalTime startTime,
                                                         @Param("endTime") java.time.LocalTime endTime);
}